<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

<!--    <el-empty description="当前页面正在开发中，敬请期待……"></el-empty>-->
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="编号" width="150" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="150" align="center"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" width="150" align="center"></el-table-column>
      <el-table-column prop="logo" label="logo" width="150" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" width="150" align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键词列表" width="150" align="center"></el-table-column>
      <el-table-column prop="sort" label="自定义排序" width="150" align="center"></el-table-column>
      <el-table-column prop="sales" label="销量" width="150" align="center"></el-table-column>
      <el-table-column prop="product_count" label="商品种类的数量" width="150" align="center"></el-table-column>
      <el-table-column prop="comment_count" label="买家评论数量" width="150" align="center"></el-table-column>
      <el-table-column prop="positive_comment_count" label="买家好评数量" width="50" align="center"></el-table-column>
      <el-table-column prop="enable"label="是否启用" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="changEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="gmt_create" label="数据创建时间" width="50"  align="center"></el-table-column>
      <el-table-column prop="gmt_modified" label="数据最后修改时间"  width="50"  align="center"></el-table-column>


      <!--      <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>-->
<!--      <el-table-column prop="albumId" label="相册ID" align="center"></el-table-column>-->
<!--      <el-table-column prop="url" label="图片url" align="center"></el-table-column>-->
<!--      <el-table-column prop="isCover"label="是否为封面图片" align="center"></el-table-column>-->
<!--      <el-table-column prop="description" label="图片简介"  header-align="center"></el-table-column>-->
<!--      <el-table-column prop="width" label="图片宽度"  header-align="center"></el-table-column>-->
<!--      <el-table-column prop="height" label="图片高度"  header-align="center"></el-table-column>-->
<!--      <el-table-column prop="sort" label="自定义排序"  align="center"></el-table-column>-->
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="openDelectConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
    }
  },
  methods:{
    changEnable(admin){
      let enableText = ['禁用', '启用'];
      let url = 'http://localhost:9080/brands/' + admin.id;
      if(admin.enable==1){
        console.log('将启用品牌');
        url += '/enable';
      } else {
        console.log('将禁用品牌');
        url += '/disable';
      }
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let message = '将品牌【' + admin.name + '】的启用状态改为【' + enableText[admin.enable] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 40400) {
          this.loadAdminList();
        }
      });
    },
    handleEdit(brand) {
      let message = '您正在尝试编辑【' + brand.id + '-' + brand.name + '】的相册详情，抱歉，该功能尚未实现……';
      this.$alert(message, '提示', {
        confirmButtonText: '确定'
      });
    },
    handleDelect(brand){
      console.log("准备删除"+brand.id)
      let url='http://localhost:9080/brands/'+brand.id+'/delete';
      this.axios.post(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.loadAlbumList();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadAlbumList();
        }
      })
    },
    openDelectConfirm(brand){
      let message='此操作将永久删除该文件['+brand.name+'], 是否继续?';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelect(brand);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    loadAlbumList(){
      console.log('loadAlbumList方法')
      let url='http://localhost:9080/brands';
      console.log('url='+url);
      this.axios.create({
        'headers':{
          'Authorization':localStorage.getItem('jwt')
        }
      }).get(url).then((response)=>{
        let responseBody=response.data;
        this.tableData=responseBody.data;
      })
    }
  },
  mounted() {//挂载
    console.log('mounted')
    this.loadAlbumList();
  }

}
</script>